package com.lz.football.module.explore.item
{
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.lz.football.module.explore.bezier.SmoothCurve;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 *移动球员item 
	 * @author Administrator
	 * 
	 */	
	public class MoveItem //extends PlayerCenterPlayerItem
	{
		/**
		 *开始点 
		 */
		private var _start:Point;
		/**
		 *结束点 
		 */
		private var _end:Point;
		/**
		 *中间点 
		 */
		private var _mid:Point;
		//绘制曲线
		private var _smooth:SmoothCurve;
		/**
		 *子 
		 */
		private var _child:DisplayObject;
		private var _parent:DisplayObjectContainer;
		private var _tweenLite:TweenLite;
		
		
		public function MoveItem()
		{
		}
		/**
		 *设置点 
		 * @param $start
		 * @param $mid
		 * @param $end
		 * 
		 */
		public function setPoint($start:Point,$mid:Point,$end:Point):void
		{
			_smooth = new SmoothCurve();
			_start = $start;
			_mid = $mid;
			_end = $end;
			_smooth.createFromPath([$start/*,$addition1*/,$mid,/*$addition2,*/$end]);
		}
		/**
		 *设置容器 
		 * 
		 */
		public function setContain($child:DisplayObject,$parent:DisplayObjectContainer):void
		{
			_child = $child
			_parent = $parent;
			_child.scaleX = _child.scaleY = 0
			GlobalDef.GameWindowContainer.addChild(_child);
			_tweenLite = TweenLite.to(_child,0.7,{onUpdate:updata,scaleX:1,scaleY:1,onComplete:onComplete});
			_child.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveHandler);
		}
		
		protected function onRemoveHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			if(_tweenLite != null)
				_tweenLite.killVars(_child);
			_tweenLite = null;
//			if(_child != null && _child.parent)
//				_child.parent.removeChild(_child);
			_child.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveHandler);
		}
		/**
		 *更新 
		 * 
		 */
		private function updata():void
		{
			if(_tweenLite == null)return;
			var r:Number =_tweenLite.currentTime /_tweenLite.duration;
			var p:Point = _smooth.getPointByDistance(_smooth.length * r);
			_child.x = p.x;
			_child.y = p.y;
		}
		/**
		 * 
		 * 
		 */
		private function onComplete():void
		{
			_child.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveHandler);
			_smooth = null;
			_child.x = _parent.numChildren *　58;
			_child.y = 0
			_parent.addChild(_child);
			_tweenLite = null;
			
		}
	}
}